<?php
/**
 * Show upcomming shifts beloning to a worker from a date onwards
 *
 * Return upcomming shift details role name, start time, end time, status, date stamp
 *
 * @author Dawid Marciniak <dawidm@gmail.com>
 */
include_once '/../utility_definition/errorcode.php';
include_once '/../shared_feature/checkAuth.php';
include_once '/../shared_feature/DBGlobal.php';
/**
 * @param $userName string
 * @param $userPass string
 * @param $targetUser string
 * @return array|void
 */
function checkWorkersUpcommingShifts($userName,$userPass,$startDate,$targetUser)
{
	global $dbaddress;
    global $dbuser;
    global $dbpassword;
    global $dbdatabasename;
	
	//declare output and auth success/fail var, error code generated throughout
	$output = array();
	$auth = 0;
	
	if (checkAuthentication($userName, $userPass) == true) {
		$conn = mysqli_connect($dbaddress,$dbuser,$dbpassword,$dbdatabasename);
		$sql = "SELECT empID FROM paradigmshift_dev.account WHERE accName = '$targetUser'";
		$result = mysqli_query($conn, $sql);
		$row = mysqli_fetch_row($result);
		$empID = $row[0];
		
		if ($row == NULL) {
			$error = 300 + ErrorCode::failRead;
		} elseif ($userName == $targetUser){//no more auth needed
			$auth = 1;
		} else {
			//check if user is manager
			$sql = "SELECT empID FROM paradigmshift_dev.account WHERE accName = '$userName'";
			$result = mysqli_query($conn, $sql);
			$row = mysqli_fetch_object($result);
			$managerID = $row->empID;
			
			$sql = "SELECT empStatus FROM paradigmshift_dev.employees WHERE empID = '$managerID'";
			$result = mysqli_query($conn, $sql);
			$row = mysqli_fetch_object($result);
			$empStatus = $row->empStatus;
			
			if ($empStatus == 'Manager') {
				$auth = 1;
			} else {
				$error = ErrorCode::authFailRead;
			}
		}
		
	} else {
		$error = ErrorCode::authFailRead;
	}
	
	if ($auth == 1) {
			//thanks will!
			$sDate=date('y/m/d',strtotime(trim($startDate)));
			$sql="select paradigmshift_dev.account.accName,roles.roleName,
			paradigmshift_dev.roledate.startTime,
			paradigmshift_dev.roledate.endTime,
			paradigmshift_dev.roledate.shiftStatus,
			paradigmshift_dev.dates.datestamp from  
			paradigmshift_dev.employees left join (dates,roledate,account,roles) on          
			paradigmshift_dev.roledate.empID=employees.empID and
			paradigmshift_dev.dates.dateID=paradigmshift_dev.roledate.dateID and
			account.empID=employees.empID and roles.roleID=roledate.roleID 
			where dates.datestamp >= '$sDate' and employees.empID='$empID'";
			$result = mysqli_query($conn, $sql);
			
			$outty[] = array();
			//build output array, a two dimentional array of data
			while($row = mysqli_fetch_array($result)){
				$outty[] = $row;
			}
			
			$error = ErrorCode::successRead;//success
			$output[0] = $outty;
	}
	//append error code to start of code
	array_unshift($output, $error);

	return $output;
}